﻿/*******************************************************
 *  就业模块 相关操作js
 * <p>Title: jobListManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2017年8月22日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//发布用工需求
var $table, addOrEditUrl, selUnit = null, options;
var jobListManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        jobListManager.initTable();
        jobListManager.bindEvent();
        jobListManager.initSelect();
        parent.delLoadBoxs();
    },
    //初始化Table
    initTable: function () {
        options = {
            url: ctx + "/BaneJobIssue/InitJobTable",
            cutHeight: 5,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    use_status: $('#useStatus').val(),//状态
                    workType: encodeURI($('#workType').val()),//岗位
                    wageStart: $('#wageStart').val(),//起薪
                    wageEnd: $('#wageEnd').val(),//最高薪资
                }
                return params;
            },
            columns: [
            {
                radio: true
            },
	        {
	            field: '',//第一列序号
	            title: '序号',
	            align: 'center',
	            width: 50,
	            formatter: function (value, row, index) {
	                return index + 1;
	            }
	        },
            {
                field: 'work_type',
                title: '岗位',
                align: 'center'
            },
	        {
	            field: 'trade_type',
	            title: '行业',
	            align: 'center'
	        },
	        {
	            field: 'work_city',
	            title: '工种城市',
	            align: 'center'
	        },
            {
                field: 'use_wage_start',
                title: '薪资',
                align: 'center',
                formatter: function (value, row, index) {
                    if (value > 0 && row.use_wage_end === 0)
                        return value + "以上";
                    if (value===0 && row.use_wage_end === 0)
                        return "面议";
                    if (row.use_wage_end > 0)
                        return value + "-" + row.use_wage_end;
                    return "";
                }
            },
	        {
	            field: 'use_sex',
	            title: '性别',
	            align: 'center'
	        }, {
	            field: 'use_age',
	            title: '年龄',
	            align: 'center'
	        }, {
	            field: 'use_edu',
	            title: '学历',
	            align: 'center'
	        }, {
	            field: 'ageLimit',
	            title: '工作年限',
	            align: 'center'
	        }, {
	            field: 'use_major',
	            title: '专业要求',
	            align: 'center'
	        }, {
	            field: 'work_start',
	            title: '就职日期',
	            align: 'center'
	        }, {
	            field: 'issue_start_date',
	            title: '发布日期',
	            align: 'center'
	        }, {
	            field: 'use_status',
	            title: '状态',
	            align: 'center',
	            formatter: function (value, row, index) {
	                switch (value) {
	                    case 1: return "待发布"; break;
	                    case 2: return "已发布"; break;
	                    case 3: return "已结束"; break;
	                    case 4: return "已作废"; break;
	                }
	                return "-";
	            }
	        }]
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //初始化下拉
    initSelect: function () {
        $('#use_age').initSelectpicker("JobWorkAge", null, true);//年龄
        $('#work_type').initSelectpicker("JobPost", null, true);//岗位
        $('#use_edu').initSelectpicker("JobEdu", null, true);//学历
        $('#use_ageLimit').initSelectpicker("WorkExperience", null, true);//工作经验
        $('#work_city').initSelectpicker("SystemArea", null, true);//工作城市
        $('#use_welfare').initSelectpicker("JobWelfare",null,true);//福利
        $('.selectpicker').selectpicker({ size: 6 });
    },
    //绑定默认事件
    bindEvent: function () {
        //查询
        $('#btnSearch').click(function () {
            $table.bootstrapTable('refresh', options);
        });
        //详细
        $('#jobDetail').click(function () {
            var row = jobListManager.getSelect();
            if (!row) {
                layer.msg("未选中行~", { icon: 5 });
                return false;
            }
            //清空表单
            $('#orgFormTable').resetHideValidForm();
            layer.open({
                title: ['招聘详细信息', 'font-size:18px;'],
                type: 1,
                content: $('#org_form'),
                scroll: true,//是否显示滚动条、默认不显示
                btn: ['确定', '取消'],
                area: ['1050px', '98%'],
                success: function (li, o) {
                    if (row != null) {
                        $('#orgFormTable')[0].reset();//重置表单
                        $('#orgFormTable').LoadForm(row);//表单填充数据
                    }
                }
            });
        });
        //添加
        $('#jobAdd').click(function () {
            jobListManager.editForm();
        });
        //编辑
        $('#jobEdit').click(function () {
            var row = jobListManager.getSelect();
            if (!row) {
                layer.msg("未选中行~", { icon: 5 });
                return false;
            }
            jobListManager.editForm(row);
        });
        //删除
        $('#jobDel').click(function () {
            var row = $table.bootstrapTable('getSelections');
            if (row != null && row.length == 0) {
                layer.msg("未选中记录行~", { icon: 5 });
                return false;
            }
            layer.confirm('您确定要删除当前选中招聘信息吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                $.ajax({
                    type: 'post',
                    url: ctx + '/BaneJobIssue/delJobData/' + row[0].job_id,
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        if (index)
                            layer.close(index);
                        if (data.Statu === 0) {
                            layer.msg("删除成功...");
                            $table.bootstrapTable('refresh', options);
                        }
                        else
                            layer.msg(data.Msg, { icon: 5 });
                    }
                });
            }, function () { });
        });
    },
    getSelect: function () {
        var row = $table.bootstrapTable('getSelections');
        if (row != null && row.length == 0) {
            layer.msg("未选中行~", { icon: 5 });
            return false;
        }
        var $data = row[0].use_welfare,
            editData = new Array();
        if ($data.indexOf(",") > -1) {
            var temp = $data.split(',');
            $.each(temp, function (index, item) {
                editData.push(item);
            });
            row[0].use_welfare = editData;
        }
        return row[0];
    },
    //编辑、添加
    editForm: function (row) {
        var title = "添加招聘信息";
        if (row == null || row == "" || row == undefined)
            addOrEditUrl = ctx + "/BaneJobIssue/AddJobData";//新增
        else {
            title = "编辑招聘信息";
            addOrEditUrl = ctx + "/BaneJobIssue/EditJobData";//编辑
        }
        //清空表单
        $('#orgFormTable').resetHideValidForm();
        //打开编辑
        layer.open({
            title: [title, 'font-size:18px;'],
            type: 1,
            content: $('#org_form'),
            scroll: true,//是否显示滚动条、默认不显示
            btn: ['确定', '取消'],
            area: ['1050px', '98%'],
            success: function (li, o) {
                if (row != null) {
                    $('#orgFormTable')[0].reset();//重置表单
                    $('#orgFormTable').LoadForm(row);//表单填充数据
                }
            },
            yes: function (li, o) {
                if ($('#orgFormTable').valid()) {
                    var use_welfare = $('#use_welfare').val();
                    $('#useWelfareValue').val(use_welfare.join(','));
                    //验证通过
                    $('#orgFormTable').ajaxSubmit({
                        url: addOrEditUrl,
                        type: "post",
                        dataType: "json",
                        beforeSubmit: function (arr, $form, options) {
                            layer.msg("提交数据~", { icon: 1, time: 6000 });
                        },
                        success: function (result, status, xhr, $form) {
                            if (result.Statu === 0) {
                                layer.closeAll();
                                $table.bootstrapTable('refresh', options);
                                layer.msg('保存成功', { icon: 1 });
                            }
                            else
                                layer.alert(result.Msg, { icon: 5 });
                        },
                        error: function (xhr, status, error, $form) {
                            layer.msg("保存失败~", { icon: 5 });
                        }
                    });
                }
            },
            cancel: function (li, o) {

            }
        });
    }
}

